package com.qianfeng.yime.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.lidroid.xutils.BitmapUtils;
import com.qianfeng.yime.Constants;
import com.qianfeng.yime.R;
import com.qianfeng.yime.model.Job;
import com.qianfeng.yime.util.BitmapUtilsHelper;

import java.util.List;

/**
 * Created by IDEA
 * User : SL
 * on  2015/3/12.
 * 12:21
 */
public class SidelineListAdapter extends BaseAdapter {

    private Context context;
    private LayoutInflater inflater;
    private List<Job> jobs;

    private int[] payTimesId = {R.drawable.rj, R.drawable.rj, R.drawable.zj, R.drawable.yj, R.drawable.wcj};
    private String[] jobTypes = {"", "派单", "服务员", "促销员", "礼仪", "客服", "小时工", "其他"};
    private String[] payUnits = {"", "元/小时", "元/天", "元/月", "元/件"};

    public SidelineListAdapter(Context context, List<Job> jobs) {
        if (context != null) {
            this.context = context;
            inflater = LayoutInflater.from(context);
        }else {
            throw new IllegalArgumentException("context must nut null");
        }
        this.jobs = jobs;
    }

    @Override
    public int getCount() {
        int ret = 0;
        if (jobs != null) {
            ret = jobs.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {
        Object ret = null;
        if (jobs != null) {
            ret = jobs.get(position);
        }
        return ret;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = null;

        if (convertView != null) {
            ret = convertView;
        }else {
            ret = inflater.inflate(R.layout.item_sideline, parent, false);
        }

        ViewHolder holder = (ViewHolder) ret.getTag();

        if (holder == null) {
            holder = new ViewHolder();

            holder.iconHolder = (ImageView) ret.findViewById(R.id.img_item_sideline_icon);
            holder.companyNameHolder = (TextView) ret.findViewById(R.id.text_item_company_name);
            holder.sidelineNameHolder = (TextView) ret.findViewById(R.id.text_item_sideline_name);
            holder.priceHolder = (TextView) ret.findViewById(R.id.text_item_sideline_price);
            holder.priceKindHolder = (TextView) ret.findViewById(R.id.text_item_price_kind);
            holder.peopleCountHolder = (TextView) ret.findViewById(R.id.text_item_people_count);
            holder.distanceHolder = (TextView) ret.findViewById(R.id.text_item_distance);
            holder.sidelineTypeHolder = (TextView) ret.findViewById(R.id.text_item_sideline_type);
            holder.sidelinePayTypeHolder = (ImageView) ret.findViewById(R.id.img_item_sideline_pay_type);

            ret.setTag(holder);
        }

        Job job = jobs.get(position);

        String corpLogo = job.getCorpLogo();
        String corpName = job.getCorpName();
        String name = job.getName();
        int pay = job.getPay();
        int payUnit = job.getPayUnit();
        int count = job.getCount();
        int dis = job.getDistance();
        float distance = (float)dis / 1000.0f;
        int jobtypeId = job.getJobtypeId();
        int jobsettletypeId = job.getJobsettletypeId();

        holder.companyNameHolder.setText(corpName);
        holder.sidelineNameHolder.setText(name);
        holder.priceHolder.setText(Integer.toString(pay));
        holder.priceKindHolder.setText(payUnits[payUnit]);
        holder.peopleCountHolder.setText("剩余" + Integer.toString(count) + "人");
        holder.distanceHolder.setText(Float.toString(distance) + "km");
        holder.sidelineTypeHolder.setText(jobTypes[jobtypeId]);
        holder.sidelinePayTypeHolder.setImageResource(payTimesId[jobsettletypeId]);

        String url = null;

        if (corpLogo != null) {
            if (!corpLogo.startsWith("http://") && !corpLogo.startsWith("https://")){
                url = Constants.API_POINT_URL + corpLogo;
            }else {
                url = corpLogo;
            }
            BitmapUtils bitmapUtils = BitmapUtilsHelper.initUtils(context);
            bitmapUtils.configDefaultLoadingImage(R.drawable.default_logo);
            bitmapUtils.configDefaultLoadFailedImage(R.drawable.default_logo);
            bitmapUtils.display(holder.iconHolder,url);
        }
        return ret;
    }

    private class ViewHolder{
        ImageView iconHolder;
        TextView companyNameHolder;
        TextView sidelineNameHolder;
        TextView priceHolder;
        TextView priceKindHolder;
        TextView peopleCountHolder;
        TextView distanceHolder;
        TextView sidelineTypeHolder;
        ImageView sidelinePayTypeHolder;
    }
}
